package oop.core.visitors;

import java.util.Random;

import oop.core.entity.Entity;
import oop.core.entity.NPC;
import oop.core.item.TakeableItem;
import oop.core.med.DelayMediator;
import oop.core.skill.Enchantment;
import oop.core.skill.PickPocket;
import oop.core.skill.Sleep;

/**
 * Visitor for the behavior of iteractive skills.
 */
public class InteractiveVisitor {
	private Entity caster;
	private NPC target;
	private DelayMediator dm;
	
	/**
	 * Construct InteractiveVisitor.
	 * @param caster
	 * @param target
	 * @param dm
	 */
	public InteractiveVisitor(Entity caster, NPC target, DelayMediator dm){
		this.caster = caster;
		this.target = target;
		this.dm = dm;
	}
	
	/**
	 * PickPocket: take a random item from target and give
	 * it to the caster.  If it fails, target becomes hostile.
	 * @param pp
	 */
	public void visitPP(PickPocket pp){
		int enemyLevel = target.getDerStats().getLevel();
		int yourLevel = caster.getDerStats().getLevel();
		int diff = yourLevel - enemyLevel;
		if(diff >= 0){
			int skillLevel = pp.getSkillLevel();
			Random rand = new Random();
			// assume skill levels 1-20 - vary the numbers as necessary
			int randomizer = 20 / skillLevel;
			int prob = rand.nextInt(randomizer);
			if(prob == 0){
				TakeableItem item = target.getInventory().getRandomItem();
				caster.addToInventory(item);
			}
			else
				target.setHostile(true);
		}
	}
	
	/**
	 * Sleep: put the target to sleep for a duration of time
	 * based on your skill level in Sleep.  If it fails, target
	 * becomes hostile.
	 * @param s
	 */
	public void visitSleep(Sleep s){
		Random rand = new Random();
		int skillLevel = s.getSkillLevel();
		int randomizer = 20 / skillLevel;
		int prob = rand.nextInt(randomizer);
		if(prob == 0){
			int delay = skillLevel * s.getDelayDuration();
			dm.register(target, delay);
		}
		else
			target.setHostile(true);
	}
	
	/**
	 * Enchantment: try to make the enemy more favoriable
	 * to the caster.  If it fails, target becomes hostile.
	 * @param e
	 */
	public void visitEnchantment(Enchantment e){
		Random rand = new Random();
		int skillLevel = e.getSkillLevel();
		int randomizer = 20 / skillLevel;
		int prob = rand.nextInt(randomizer);
		if(prob == 0){
			int bonus = skillLevel * 2 ;
			target.modifyFavoribility(bonus);
		}
		else
			target.setHostile(true);
	}

	/**
	 * get/set methods
	 */
	public Entity getCaster() {
		return caster;
	}

	public void setCaster(Entity caster) {
		this.caster = caster;
	}

	public NPC getTarget() {
		return target;
	}

	public void setTarget(NPC target) {
		this.target = target;
	}

	public DelayMediator getDm() {
		return dm;
	}

	public void setDm(DelayMediator dm) {
		this.dm = dm;
	}
}
